﻿using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomSecurity;

public partial class AccountSettings : System.Web.UI.Page
{
    private int AppUserID
    {
        get
        {
            return ((CustomIdentity)Context.User.Identity).AppUserID;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Hashtable oAppUserInfo = null;
        int prefWtUnitID = -1, prefHtUnitID = -1;
        using (AppUser oRead = new AppUser())
        {
            oAppUserInfo = oRead.GetAppUserInfo(AppUserID);
            using (DataTable dt = oRead.GetMeasurementPreference(AppUserID))
            {
                prefHtUnitID = Convert.ToInt32(dt.Select("MeasurementTypeID = 1")[0]["MeasurementUnitID"]);
                prefWtUnitID = Convert.ToInt32(dt.Select("MeasurementTypeID = 2")[0]["MeasurementUnitID"]);
            }
        }
        if (!IsPostBack)
        {
            hdnReturnURL.Value = Request.UrlReferrer.LocalPath;
            txtFirstName.Text = Convert.ToString(oAppUserInfo["FirstName"]);
            txtLastName.Text = Convert.ToString(oAppUserInfo["LastName"]);
            txtEmailAddress.Text = Convert.ToString(oAppUserInfo["EmailAddress"]);

            using (Measurement oMeasurement = new Measurement())
            {
                Hashtable oParam = new Hashtable();
                
                using (DataView dv = oMeasurement.GetMeasurementUnits(oParam).DefaultView)
                {
                    dv.RowFilter = "MeasurementTypeID = 1 AND Level < 2";
                    ddPrefHeightUnit.DataValueField = "MeasurementUnitID";
                    ddPrefHeightUnit.DataTextField = "MeasurementUnitFullName";
                    ddPrefHeightUnit.DataSource = dv;
                    ddPrefHeightUnit.DataBind(); 
                    ddPrefHeightUnit.SelectedValue = prefHtUnitID.ToString();

                    dv.RowFilter = "MeasurementTypeID = 2 AND Level < 2";
                    ddPrefWeightUnit.DataValueField = "MeasurementUnitID";
                    ddPrefWeightUnit.DataTextField = "MeasurementUnitFullName";
                    ddPrefWeightUnit.DataSource = dv;
                    ddPrefWeightUnit.DataBind();
                    ddPrefWeightUnit.SelectedValue = prefWtUnitID.ToString();

                    dv.RowFilter = "";
                    using (DataSet ds = MJHDataService.DataSetUtility.TransformFlatToHierarchy(dv.Table, 2, "MeasurementUnitID", "ParentMeasurementUnitID"))
                    {
                        foreach (DataTable dt in ds.Tables)
                        {
                            using (DataGrid dg = new DataGrid())
                            {
                                dg.DataSource = dt;
                                dg.DataBind();
                                phDisplayDataSet.Controls.Add(dg);
                            }
                        }
                    }
                }
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        using (AppUser oSvc = new AppUser())
        {
            Hashtable oAppUserInfo = oSvc.GetAppUserInfo(AppUserID);
            oAppUserInfo["FirstName"] = txtFirstName.Text;
            oAppUserInfo["LastName"] = txtLastName.Text;
            oAppUserInfo["EmailAddress"] = txtEmailAddress.Text;

            oSvc.UpdateInfo(oAppUserInfo);

            oSvc.SaveMeasurementPreference(AppUserID, int.Parse(ddPrefHeightUnit.SelectedValue));
            oSvc.SaveMeasurementPreference(AppUserID, int.Parse(ddPrefWeightUnit.SelectedValue));
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(hdnReturnURL.Value);
    }
}
